﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace Master
{
    /**
     * TrafficLightStrategyFactory is responsible for instantiating traffic light strategies.
     * This factory contains a create-method for every possible trafficlightstrategy.
     * Additionally, strategies can be created by using the 'createStrategy'-method and passing the strategyName (in string-form) as parameter
     * 
     * @Author A.J. Janssen
     * */
    public static class TrafficLightStrategyFactory
    {
        /**************
         * REFLECTION *
         **************/

        public readonly static Dictionary<String, MethodInfo> methodDictionary;

        static TrafficLightStrategyFactory()
        {
            methodDictionary = new Dictionary<String, MethodInfo>();
            foreach (MethodInfo method in typeof(TrafficLightStrategyFactory).GetMethods())
            {
                if (method.Name == "createStrategy")
                    continue;
                if (method.Name.StartsWith("create"))
                    methodDictionary.Add(method.Name.Substring(6), method);
            }
        }

        public static iTrafficLightStrategy createStrategy(String strategyName)
        {
            if (!methodDictionary.ContainsKey(strategyName)) throw new InvalidOperationException("No such strategy exists");
            return (iTrafficLightStrategy)methodDictionary[strategyName].Invoke(null, null);
        }

        /*********************
         * STRATEGY CREATION *
         *********************/

        public static iTrafficLightStrategy createSimpleStrategy()
        {
            return new SimpleTrafficLightStrategy();
        }

        public static iTrafficLightStrategy createTwoWayStrategy()
        {
            return new TwoWayTrafficLightStrategy();
        }

        public static iTrafficLightStrategy createThreeLaneStrategy()
        {
            return new ThreeLaneTrafficLightStrategy();
        }
        public static iTrafficLightStrategy createTJunctionStrategy()
        {
            return new TJunctionStrategy();
        }

        
    }
}
